import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'
import AutoImport from 'unplugin-auto-import/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import { VantResolver } from '@vant/auto-import-resolver'
import Components from 'unplugin-vue-components/vite'
import legacy from '@vitejs/plugin-legacy'
import vueDevTools from 'vite-plugin-vue-devtools'
import Icons from 'unplugin-icons/vite'
import IconsResolver from 'unplugin-icons/resolver'
import { qrcode } from 'vite-plugin-qrcode'
import { viteMockServe } from 'vite-plugin-mock'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    qrcode(),
    // 接口数据模拟
    viteMockServe({
      mockPath: 'mock' // 存放接口模拟数据的文件夹
    }),
    // 打包完成后上传cdn插件
    // uploadCdnVitePlugin(cdnConfig),
    // 图标库插件
    Icons({ compiler: 'vue3', autoInstall: true }),
    // 兼容低版本浏览器处理
    legacy({
      targets: ['defaults', 'not IE 11', 'chrome 55']
    }),
    vueDevTools({
      launchEditor: 'webstorm'
    }),
    // 自动导入
    AutoImport({
      // 可以自定义文件生成的位置，默认是根目录下，使用ts的建议放src目录下
      dts: 'src/auto-imports.d.ts',
      imports: ['vue', 'vue-router'],
      resolvers: [ElementPlusResolver(), VantResolver()],
      eslintrc: {
        enabled: true
      }
    }),
    // vue组件按需自动导入
    Components({
      resolvers: [ElementPlusResolver(), VantResolver(), IconsResolver()]
    })
  ],
  resolve: {
    alias: {
      '@': resolve(__dirname, '.', 'src')
    }
  },
  build: {
    outDir: process.env.npm_package_name,
    emptyOutDir: true
  },
  css: {
    preprocessorOptions: {
      scss: {
        additionalData: `@use "@/assets/style/global.scss" as *;@use "@/assets/style/theme/index.scss" as *;`,
        api: 'modern-compiler'
      }
    }
  },
  server: {
    host: '0.0.0.0'
  },
  base:
    process.env.NODE_ENV === 'development'
      ? './'
      : `//cdnver.13jy.cn/v1/${process.env.npm_package_name}/`
})
